package cn.yshujia.service.impl;

import cn.yshujia.domain.LoginUser;
import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Router;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.domain.vo.RouterVO;
import cn.yshujia.domain.vo.UserVO;
import cn.yshujia.mapper.RouterMapper;
import cn.yshujia.repository.RouterRepository;
import cn.yshujia.utils.PageUtils;
import cn.yshujia.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 服务层
 */

@Slf4j
@Service
public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> implements IService<Router> {
	
	@Resource
	RouterRepository repository;
	
	@Resource
	RouterMapper mapper;
	
	@Resource
	UserServiceImpl userService;
	
	public List<RouterVO> getList() {
		return repository.getRoutersByRoleId(600000L);
	}
	
	public List<RouterVO> getList(Long userId) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		if (null != loginUser) {
			return repository.getRoutersByRoleId(loginUser.getUser().getRole().getId());
		}
		UserVO userVO = userService.getUser(userId);
		return repository.getRoutersByRoleId(userVO.getRole().getId());
	}
	
	public PageVO<RouterVO> getAdminPage(PageDTO dto) {
		Map<String, Object> map = dto.getParam();
		if (ObjectUtils.isEmpty(map)) {
			return PageUtils.getPage(dto, new ArrayList<>());
		}
		// 查询满足条件的路由
		QueryWrapper<Router> wrapper = PageUtils.createRouterWrapper(dto);
		wrapper.ge("role_id", map.get("roleId"));
		IPage<Router> page = mapper.selectPage(new Page<>(dto.getCurrent(), dto.getSize()), wrapper);
		
		if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getRecords())) {
			return PageUtils.getPage(dto, repository.createVOList(page.getRecords()), page.getTotal());
		}
		return new PageVO<>();
	}
	
	public List<Router> getOneLevelByIsAdmin(Boolean isAdmin) {
		return mapper.selectList(new LambdaQueryWrapper<Router>()
				.select(Router::getId, Router::getTitle)
				.eq(Router::getIsBar, true)
				.eq(Router::getLevel, 1)
				.eq(Router::getIsAdmin, isAdmin));
	}
	
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean insert(Router router) {
		router.setId(null);
		int n = mapper.insert(router);
		if (n > 0) {
			repository.del();
			return true;
		}
		return false;
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean update(Router router) {
		int n = mapper.updateById(router);
		if (n > 0) {
			repository.del();
			return true;
		}
		return false;
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean remove(List<Long> ids) {
		int n = mapper.deleteByIds(ids);
		if (n > 0) {
			repository.del();
			return true;
		}
		return false;
	}
}